##################################################################

# Gautam Nair
# gautam.nair@yale.edu
# Misperceptions of Relative Affluence and Support for International Transfers
# Make Table 6: Heterogeneous Effects By Priors

##################################################################

# setwd("")

##################################################################
# loading packages
##################################################################

library(Hmisc)
library(foreign)
library(sandwich)
library(lmtest)
library(numDeriv)
library(stargazer)
library(ggplot2)
library(plyr)
library(gridExtra)
library(ri)
library(dplyr)
library(plyr)
library(scales)

#################################################################
# Summary Statistics
##################################################################

rm(list=ls())

###

data.working <- readRDS("d_r_cleaned_analysis_dataset.Rda")

vars <- c(
"age",
"educat.high.school.plus",
"educat.ba.plus",
"income.cat.0.25",
"income.cat.25.50",
"income.cat.50.75",
"income.cat.75.100",
"income.cat.100.plus",
"race.white",
"race.black",
"race.hispanic",
"gender.female",
"hh.head.yes",
"pphhsize",
"married.now.yes",
"metro.area.yes",
"home.own",
"work.employed",
"work.retired",
"work.notworking.other",
"partyid.republican",
"partyid.democrat",
"partyid.independent",
"ideology.liberal",
"ideology.moderate",
"ideology.conservative",
"religion.christian"
)

varslength <- length(vars)
mean.sd <- rep(NA, varslength)

for (i in 1:length(vars)){ 
	
		temp.mean <- format(round(mean(data.working[,vars[i]], na.rm=TRUE), 2), nsmall=2)
		temp.sd <- format(round(sd(data.working[,vars[i]], na.rm=TRUE), 2), nsmall=2)
		temp.mean.sd <- paste(temp.mean, " ", "(", temp.sd, ")", sep="")
		mean.sd[i] <- temp.mean.sd
}

vars.label <- c(
"Age",
"High School or Greater Education",
"BA or Greater Education", 
"Household Income 0-25,000",
"Household Income 25-50,000", 
"Household Income 50-75,000", 
"Household Income 75-100,000",
"Household Income 100,000+",
"Race White",
"Race Black",
"Race Hispanic",
"Female",
"Household Head",
"Household Size",
"Married",
"Lives in Metro",
"Own Home",
"Employed",
"Retired",
"Not Working/Other",
"Republican",
"Democrat",
"Independent",
"Liberal",
"Moderate",
"Conservative",
"Christian"
)

working.table <- as.data.frame(cbind(vars.label, mean.sd))
colnames(working.table) <- c("Variable", "Mean/Proportion (SD)")
table.title <- c("Table 7: Summary Statistics")
output.file <- c("tf_t_07_summary_statistics")
stargazer(working.table, 
title = table.title,
out = output.file,
type= "text",
summary=FALSE, 
rownames=FALSE)

